﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace ThanNhanTao.Server.BLL
{
    public class Dm_YeuCauBLL
    {
        bvdn_thannhantaoEntities entities;
        public Dm_YeuCauBLL()
        {
            entities = ThanNhanTaoGlobalServer.bvdn_thannhantaoEntities;
        }
        public List<Dm_YeuCau> RetrieveAllDmYeuCau()
        {
            List<Dm_YeuCau> listYeuCau = new List<Dm_YeuCau>();
            listYeuCau = (from yeuCau in entities.Dm_YeuCau
                                        .Include("Dm_DonViTinh")
                                        .Include("Dm_LoaiYeuCau")
                          orderby yeuCau.MaYeuCau
                          select yeuCau).ToList();
            return listYeuCau;
        }
        public List<Dm_YeuCau> RetrieveAllDmYeuCauChayThan()
        {
            List<Dm_YeuCau> listYeuCau = new List<Dm_YeuCau>();
            listYeuCau = (from yeuCau in entities.Dm_YeuCau
                          orderby yeuCau.TenYeuCau
                          where yeuCau.LaChayThan
                          select yeuCau).ToList();
            return listYeuCau;
        }
        public List<Dm_YeuCau> RetrieveAllDmYeuCauNgoaiTru()
        {
            List<Dm_YeuCau> listYeuCau = new List<Dm_YeuCau>();
            listYeuCau = (from yeuCau in entities.Dm_YeuCau
                                        .Include("Dm_DonViTinh")
                                        .Include("Dm_LoaiYeuCau")
                            where yeuCau.KichHoat
                                && !yeuCau.LaChayThan
                                && yeuCau.Dm_LoaiYeuCau.KichHoat
                                && yeuCau.ChoNoiTru
                            select yeuCau).ToList();
            return listYeuCau;
        }

        public List<Dm_YeuCau> RetrieveAllDmYeuCauNoiTru()
        {
            List<Dm_YeuCau> listYeuCau = new List<Dm_YeuCau>();
            listYeuCau = (from yeuCau in entities.Dm_YeuCau
                                        .Include("Dm_DonViTinh")
                                        .Include("Dm_LoaiYeuCau")
                          where  yeuCau.KichHoat
                              && !yeuCau.LaChayThan
                              && yeuCau.Dm_LoaiYeuCau.KichHoat
                              && yeuCau.ChoNgoaiTru
                          select yeuCau).ToList();
            return listYeuCau;
        }

        public List<Dm_YeuCau> RertieveAllDichTruyenThuocNoiTru()
        {
            List<Dm_YeuCau> listYeuCau = new List<Dm_YeuCau>();
            listYeuCau = (from yeuCau in entities.Dm_YeuCau
                                        .Include("Dm_LoaiYeuCau")
                          where yeuCau.KichHoat
                              && !yeuCau.LaChayThan
                              && yeuCau.Dm_LoaiYeuCau.KichHoat
                              && yeuCau.ChoNgoaiTru
                              && yeuCau.MaLoaiYeuCau == "THCDT"
                          select yeuCau).ToList();
            return listYeuCau;
        }


        public int AddDmYeuCau(Dm_YeuCau dmYeuCau)
        {
            try
            {
                entities.AddToDm_YeuCau(dmYeuCau);
                return entities.SaveChanges();
            }
            catch (Exception)
            {
            }
            return -1;
        }
        public int UpdateDmYeuCau(Dm_YeuCau dmYeuCau)
        {
            try
            {
                Dm_YeuCau temple = GetIdDmYeuCau(dmYeuCau.MaYeuCau);
                temple.MaYeuCau = dmYeuCau.MaYeuCau;
                temple.MaLoaiYeuCau = dmYeuCau.MaLoaiYeuCau;
                temple.MaDonViTinh = dmYeuCau.MaDonViTinh;
                temple.LaChayThan = dmYeuCau.LaChayThan;
                temple.ChoNoiTru = dmYeuCau.ChoNoiTru;
                temple.ChoNgoaiTru = dmYeuCau.ChoNgoaiTru;
                temple.PhanLoaiBHYT = 0;
                temple.GiaYeuCau = dmYeuCau.GiaYeuCau;
                temple.GiaKhac = dmYeuCau.GiaKhac;
                temple.QuyBHYT = dmYeuCau.QuyBHYT;
                temple.TenYeuCau = dmYeuCau.TenYeuCau;
                temple.GhiChu = dmYeuCau.GhiChu;
                temple.KichHoat = dmYeuCau.KichHoat;
                return entities.SaveChanges();
            }
            catch
            { }
            return -1;
        }
        public bool DeleteDmYeuCau(string msYeuCau)
        {
            try
            {
                entities.DeleteObject(GetIdDmYeuCau(msYeuCau));
                entities.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        public Dm_YeuCau GetIdDmYeuCau(string maYeuCau)
        {
            Dm_YeuCau DmYeuCauId;

            DmYeuCauId = (from c in entities.Dm_YeuCau where c.MaYeuCau == maYeuCau select c).SingleOrDefault();

            return DmYeuCauId;
        }

        public List<Dm_LoaiYeuCau> GetDSLoaiYeuCau()
        {
            List<Dm_LoaiYeuCau> listLoaiYeuCau = new List<Dm_LoaiYeuCau>();
            listLoaiYeuCau = (from loaiYeuCau in entities.Dm_LoaiYeuCau
                              select loaiYeuCau).ToList();
            return listLoaiYeuCau;
        }

        public List<Dm_DonViTinh> GetDSDonViTinh()
        {
            List<Dm_DonViTinh> listDonViTinh = new List<Dm_DonViTinh>();
            listDonViTinh = (from donViTinh in entities.Dm_DonViTinh
                             select donViTinh).ToList();
            return listDonViTinh;
        }
    }
}
